package com.chainyoung.trademarket.ui;

import android.text.TextUtils;

import com.chainyoung.common.base.WebViewActvity;
import com.chainyoung.common.di.AppComponent;
import com.chainyoung.trademarket.di.component.DaggerMainComponent;
import com.chainyoung.trademarket.di.module.MainModule;
import com.chainyoung.trademarket.entity.Ad;
import com.chainyoung.trademarket.mvp.contract.AdDetailContract;
import com.chainyoung.trademarket.mvp.presenter.AdDetailPresenter;

import java.util.TreeMap;

public class AdDetailActivity extends WebViewActvity<AdDetailPresenter> implements AdDetailContract.View {


    private Ad ad;
    private String title;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerMainComponent.builder()
                .appComponent(appComponent)
                .mainModule(new MainModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void initUI() {
        super.initUI();
        ad = getIntent().getParcelableExtra("ad");
        title=getIntent().getStringExtra("title");
        if (!TextUtils.isEmpty(title)){
            setCurrentTitle(title.length()>10?title.substring(0,10)+"...":title);
        }
    }

    @Override
    protected void initData() {
        if (ad!=null){
        TreeMap<String,Object> map=new TreeMap<>();
        map.put("id",ad.getId());
        map.put("from_table",ad.getFrom_table());
        mPresenter.getAdDetail(map);}
    }

    @Override
    protected int getLayoutId() {
        return super.getLayoutId();
    }

    @Override
    public void onGetAdDetail(Ad data) {
        setCurrentTitle(data.getTitle().length()>10?data.getTitle().substring(0,10)+"...":data.getTitle());
        mWebView.getSettings().setDefaultFontSize(40);
        mWebView.loadDataWithBaseURL(null,data.getContent(),"text/html","UTF-8",null);
    }
}
